package com.mall.mapper

import com.mall.entity.Admin
import org.apache.ibatis.annotations.*

/**
 * 管理员数据访问接口
 */
@Mapper
interface AdminMapper {
    
    /**
     * 创建管理员
     */
    @Insert("INSERT INTO admins (username, password, email, role, status, created_at, updated_at) " +
            "VALUES (#{username}, #{password}, #{email}, #{role}, #{status}, now(), now())")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    fun insert(admin: Admin): Int
    
    /**
     * 根据用户名查找管理员
     */
    @Select("SELECT * FROM admins WHERE username = #{username} AND deleted_at IS NULL")
    fun findByUsername(username: String): Admin?
    
    /**
     * 更新管理员信息
     */
    @Update("UPDATE admins SET username = #{username}, password = #{password}, " +
            "email = #{email}, role = #{role}, status = #{status}, updated_at = now() " +
            "WHERE id = #{id} AND deleted_at IS NULL")
    fun update(admin: Admin): Int
    
    /**
     * 删除管理员（软删除）
     */
    @Update("UPDATE admins SET deleted_at = now() WHERE id = #{id} AND deleted_at IS NULL")
    fun delete(id: Long): Int
    
    /**
     * 获取管理员总数
     */
    @Select("SELECT COUNT(*) FROM admins WHERE deleted_at IS NULL")
    fun count(): Long
    
    /**
     * 获取管理员列表（分页）
     */
    @Select("SELECT * FROM admins WHERE deleted_at IS NULL LIMIT #{offset}, #{pageSize}")
    fun findAll(@Param("offset") offset: Int, @Param("pageSize") pageSize: Int): List<Admin>
}